package com.sugon.controller.device;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.hutool.core.collection.CollectionUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.entity.SpecialDevSafetyValveEntity;
import com.sugon.modules.excel.IExcelHandler;
import com.sugon.service.SpecialDevSafetyValveService;
import com.sugon.service.SysConfigService;
import com.sugon.utils.MapUtil;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 特种设备-安全阀台账Controller
 *
 * @author sugon
 * @date 2021-09-08 16:30:40
 */
@Controller
@RequestMapping("specialdevsafetyvalve")
public class SpecialDevSafetyValveController {
    @Autowired
    private SpecialDevSafetyValveService specialDevSafetyValveService;
    @Autowired
    @Qualifier("safetyValveExcelHandler")
    private IExcelHandler excelHandler;
    @Autowired
    private SysConfigService configService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<SpecialDevSafetyValveEntity> specialDevSafetyValveList = specialDevSafetyValveService.queryList(query);
        int total = specialDevSafetyValveService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(specialDevSafetyValveList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        SpecialDevSafetyValveEntity specialDevSafetyValve = specialDevSafetyValveService.queryObject(id);

        return R.ok().put("specialDevSafetyValve", specialDevSafetyValve);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody SpecialDevSafetyValveEntity specialDevSafetyValve) {
        specialDevSafetyValveService.save(specialDevSafetyValve);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody SpecialDevSafetyValveEntity specialDevSafetyValve) {
        specialDevSafetyValveService.update(specialDevSafetyValve);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        specialDevSafetyValveService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<SpecialDevSafetyValveEntity> list = specialDevSafetyValveService.queryList(params);

        return R.ok().put("list", list);
    }

    @RequestMapping("/import")
//    @RequiresPermissions("specialDevice:imports")
    @ResponseBody
    public R specialDeviceImports(@RequestParam("file") MultipartFile file) throws Exception {
        if (file == null || file.isEmpty()) {
            return R.error("上传文件不能为空！");
        }
        int rows = excelHandler.importExcelFromStream(file.getInputStream());
        return R.ok("成功导入" + rows + "记录");
    }

    @RequestMapping("/export")
    @ResponseBody
    public void export(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {
        // 查询工装清单列表
        MapUtil.removeEmptyValue(params);
        List<SpecialDevSafetyValveEntity> safetyValveList = specialDevSafetyValveService.queryList(params);
        if (CollectionUtil.isNotEmpty(safetyValveList)) {
            for (int i = 0; i < safetyValveList.size(); i++) {
                safetyValveList.get(i).setIdx(i + 1);
            }
        }
        Map<String, Object> map = new HashMap<>(1);
        map.put("list", safetyValveList);
        // 构建模板导出参数
        TemplateExportParams templateExportParams = new TemplateExportParams(configService.getPoiTemplatePath() + "safetyValueIETemplate.xlsx");
        templateExportParams.setColForEach(true);

        modelMap.put(TemplateExcelConstants.FILE_NAME, "安全阀_" + com.sugon.modules.utils.DateUtil.today());
        modelMap.put(TemplateExcelConstants.PARAMS, templateExportParams);
        modelMap.put(TemplateExcelConstants.MAP_DATA, map);
        PoiBaseView.render(modelMap, request, response, TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
    }

}
